/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generateAndLoadClasses;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;

import Utils.Proprietati;

/**
 * Create grammar java file and compile them 
 * @author daniel
 */


public class CompileGrammar {
	public static void generateJavas( String path) throws IOException{
		generateJavas(path, Proprietati.genJavaOut);
	}

	public static void generateJavas( String grammarPath , String outputPath) throws IOException{
		// construct the command for crating java files from grammar file
		//copy needed classed (Tree and TreeNode)
		//FileUtils.copyFile(new File(Properties.classLoaderRoot+"\\"+"Tree.java"),new File(Properties.genJavaOut+"\\"+"Tree.java") );
		//FileUtils.copyFile(new File(Properties.classLoaderRoot+"\\"+"TreeNode.java"),new File(Properties.genJavaOut+"\\"+"TreeNode.java") );
		String command = "java -XX:MaxPermSize=256m -jar "+ Proprietati.antlrPath+" -fo "+ outputPath + " " + grammarPath;
		BufferedReader is;  // reader for output of process
		String line;
		try {
			Process p = Runtime.getRuntime().exec(command);
			is = new BufferedReader(new InputStreamReader(p.getErrorStream()));

			while ((line = is.readLine()) != null)
				System.out.println(line);
			System.out.flush();
			
			try {
				System.out.println(p.waitFor());
				System.out.println(p.exitValue());
			} catch (InterruptedException ex) {
				Logger.getLogger(CompileGrammar.class.getName()).log(Level.SEVERE, null, ex);
				
			}
		} catch (IOException ex) {
			Logger.getLogger(CompileGrammar.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void compileClasses( String filename){
		// construct the command for creating class file from generated java files
		String command = Proprietati.javac+" -classpath \""+ Proprietati.antlrPath+ ";"+Proprietati.thisjar+"\" -d \""+Proprietati.genJavaOut+"\" \""+filename+"\" " ;
		System.out.println(command);
		BufferedReader is;  // reader for output of process
		String line;
		try {
			Process p = Runtime.getRuntime().exec(command);
			is = new BufferedReader(new InputStreamReader(p.getErrorStream()));

			while ((line = is.readLine()) != null)
				System.out.println(line);

			System.out.flush();
			try {
				System.out.println(p.waitFor());
				System.out.println(p.exitValue());
			} catch (InterruptedException ex) {
				Logger.getLogger(CompileGrammar.class.getName()).log(Level.SEVERE, null, ex);
			}
		} catch (IOException ex) {
			Logger.getLogger(CompileGrammar.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void compileClasses( String filePath,String outputpath ){
		// construct the command for creating class file from generated java files
		String command = Proprietati.javac+" -classpath "+ Proprietati.antlrPath+ " -d "+outputpath+" "+filePath ;
		System.out.println(command);
		BufferedReader is;  // reader for output of process
		String line;
		try {
			Process p = Runtime.getRuntime().exec(command);
			is = new BufferedReader(new InputStreamReader(p.getErrorStream()));

			while ((line = is.readLine()) != null)
				System.out.println(line);

			System.out.flush();
			try {
				System.out.println(p.waitFor());
				System.out.println(p.exitValue());
			} catch (InterruptedException ex) {
				Logger.getLogger(CompileGrammar.class.getName()).log(Level.SEVERE, null, ex);
			}
		} catch (IOException ex) {
			Logger.getLogger(CompileGrammar.class.getName()).log(Level.SEVERE, null, ex);
		}
	}


	public static void main (String [] a){
		//compileClasses(null);
		String path = CompileGrammar.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		try {
			String decodedPath = URLDecoder.decode(path, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
